package com.znufe.exam.controller.student;

import java.util.Date;

import com.znufe.exam.base.BaseApiController;
import com.znufe.exam.viewmodel.student.autopage.AutoPageCreateVm;
import com.znufe.exam.viewmodel.student.autopage.AutoPageRequestVm;
import com.znufe.exam.viewmodel.student.autopage.AutoPageResponseVm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.znufe.exam.domain.AutoPageSpan;
import com.znufe.exam.service.AutoPageSpanService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.znufe.exam.base.RestResponse;
import com.znufe.exam.base.SystemCode;

import javax.validation.Valid;

/**   
 * 
 * @Description:  自动翻页设置表接口层
 * @Author:          
 * @CreateDate:   Course
 * @Version:      V1.0
 *    
 */
@Api(description = "自动翻页设置表",value="自动翻页设置表" )
@RestController
@RequestMapping("/api/user/auto")
public class AutoPageSpanController extends BaseApiController {

	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	public AutoPageSpanService autoPageSpanService;


	@PostMapping("/getSetting")
    @ApiOperation(value = "根据课程，班级，试卷ID查找自动翻页设置",notes = "返回自动翻页信息")
    public RestResponse<AutoPageResponseVm> getAutoPageSetting(@RequestBody AutoPageRequestVm requestVm){
        AutoPageSpan autoPageSpan = autoPageSpanService.selcetByIds(requestVm);
        if(autoPageSpan == null){
            return RestResponse.fail(SystemCode.NoDataExist.getCode(),SystemCode.NoDataExist.getMessage());
        }else {
            AutoPageResponseVm responseVm = AutoPageResponseVm.from(autoPageSpan);
            return RestResponse.ok(responseVm);
        }
    }

    @PostMapping("/edit")
    @ApiOperation(value = "用户端：新建与修改自动翻页设置",notes = "新建时ID设为null，修改时需要传该条设置ID")
    public RestResponse<AutoPageSpan> edit(@RequestBody @Valid AutoPageCreateVm createVm){
	    if (createVm.getId() == null){
	        AutoPageSpan existSpan = autoPageSpanService.selectByClassCoursePaperId(createVm.getClassId(),
                    createVm.getCourseId(),createVm.getExamPaperId());
	        if(null != existSpan){
	            return RestResponse.fail(421,"自动翻页设置已存在");
            }
        }
        AutoPageSpan newSpan = modelMapper.map(createVm,AutoPageSpan.class);
	    if (createVm.getId() == null){
	        newSpan.setGmtCreate(new Date());
	        int reg = autoPageSpanService.insertSelective(newSpan);
	        if (reg > 0){
                return RestResponse.ok(newSpan);
            }else {
	            return RestResponse.fail(SystemCode.InsertDataFail.getCode(),SystemCode.InsertDataFail.getMessage());
            }
        }else {
	        newSpan.setGmtModified(new Date());
	        int reg = autoPageSpanService.updateByPrimaryKeySelective(newSpan);
	        if (reg >0 ){
                return RestResponse.ok(newSpan);
            }else {
	            return RestResponse.fail(SystemCode.UpdateDataFail.getCode(),SystemCode.UpdateDataFail.getMessage());
            }
        }
    }
}